﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using TransFormer.Common;
using TransFormer.Model;
using KeyWords = TransFormer.BLL.KeyWords;

namespace TransFormer.Web
{
    public partial class Product : System.Web.UI.Page
    {
        public List<ProductType> ProductTypes;
        public List<TransFormer.Model.Product> Products;
        private TransFormer.BLL.ProductType _bllProductType = new BLL.ProductType();
        private TransFormer.BLL.Product _bllProduct = new BLL.Product();

        public string PageStr;
        private BLL.KeyWords _bllKey = new KeyWords();
        protected void Page_Load(object sender, EventArgs e)
        {
            var model = _bllKey.GetModel(3);
            if (model != null)
            {
                HtmlMeta keywords = new HtmlMeta();
                keywords.Name = "keywords";
                keywords.Content = model.KeyContent;
                Page.Header.Controls.Add(keywords);
                HtmlMeta desc = new HtmlMeta();
                desc.Name = "description";
                desc.Content = model.PageDesc;
                Page.Header.Controls.Add(desc);
                Title = model.Title;

            }


            const int pageSize = 3;
            int pageIndex = 1;
            int.TryParse(Request["pageIndex"], out pageIndex);
            int count = 0;
            //if (IsPostBack) return;
            ProductTypes = _bllProductType.GetModelList("Language='english'");

            string productTypeId = Request["ProductTypeId"] ?? string.Empty;
            if (Regex.IsMatch(productTypeId, @"[^\d]+"))
                Response.Redirect("product.aspx");
            if (string.IsNullOrEmpty(productTypeId))
            {
                Products = _bllProduct.GetModelList(string.Empty)
                    .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                count = _bllProduct.GetRecordCount(string.Empty);
                PageStr = LaomaPager.ShowPageNavigate(pageSize, pageIndex, count, "product.aspx?id=0");
            }
            else
            {
                Products = _bllProduct.GetModelList("Type=" + productTypeId)
                    .Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                count = _bllProduct.GetRecordCount("Type=" + productTypeId);
                PageStr = LaomaPager.ShowPageNavigate(pageSize, pageIndex, count, "product.aspx?productTypeId=" + productTypeId);
            }
        }
/*        protected void search_Click(object sender, EventArgs e)
        {
            Response.Redirect("productSearch.aspx?searchInput=" + searchInput.Text);
        }*/
    }
}